{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上一个代码的输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "sqls = [{'question_type': 'check_disease', 'sql': [\"MATCH (m:Disease)-[r:need_check]->(n:Check) where n.name = '血常规' return m.name, r.name, n.name\"]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2neo import Graph"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 连接图数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = Graph(\"http://localhost:7474\", auth=(\"neo4j\", \"111695\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_limit = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "final_answers = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'question_type': 'check_disease',\n",
       "  'sql': [\"MATCH (m:Disease)-[r:need_check]->(n:Check) where n.name = '血常规' return m.name, r.name, n.name\"]}]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sqls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''根据对应的qustion_type，调用相应的回复模板'''\n",
    "def answer_prettify(question_type, answers):\n",
    "    final_answer = []\n",
    "    if not answers:\n",
    "        return ''\n",
    "    if question_type == 'disease_symptom':\n",
    "        desc = [i['n.name'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}的症状包括：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'symptom_disease':\n",
    "        desc = [i['m.name'] for i in answers]\n",
    "        subject = answers[0]['n.name']\n",
    "        final_answer = '症状{0}可能染上的疾病有：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_cause':\n",
    "        desc = [i['m.cause'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}可能的成因有：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_prevent':\n",
    "        desc = [i['m.prevent'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}的预防措施包括：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_lasttime':\n",
    "        desc = [i['m.cure_lasttime'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}治疗可能持续的周期为：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_cureway':\n",
    "        desc = [';'.join(i['m.cure_way']) for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}可以尝试如下治疗：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_cureprob':\n",
    "        desc = [i['m.cured_prob'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}治愈的概率为（仅供参考）：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_easyget':\n",
    "        desc = [i['m.easy_get'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "\n",
    "        final_answer = '{0}的易感人群包括：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_desc':\n",
    "        desc = [i['m.desc'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0},熟悉一下：{1}'.format(subject,  '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_acompany':\n",
    "        desc1 = [i['n.name'] for i in answers]\n",
    "        desc2 = [i['m.name'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        desc = [i for i in desc1 + desc2 if i != subject]\n",
    "        final_answer = '{0}的症状包括：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_not_food':\n",
    "        desc = [i['n.name'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}忌食的食物包括有：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_do_food':\n",
    "        do_desc = [i['n.name'] for i in answers if i['r.name'] == '宜吃']\n",
    "        recommand_desc = [i['n.name'] for i in answers if i['r.name'] == '推荐食谱']\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}宜食的食物包括有：{1}\\n推荐食谱包括有：{2}'.format(subject, ';'.join(list(set(do_desc))[:num_limit]), ';'.join(list(set(recommand_desc))[:self.num_limit]))\n",
    "\n",
    "    elif question_type == 'food_not_disease':\n",
    "        desc = [i['m.name'] for i in answers]\n",
    "        subject = answers[0]['n.name']\n",
    "        final_answer = '患有{0}的人最好不要吃{1}'.format('；'.join(list(set(desc))[:num_limit]), subject)\n",
    "\n",
    "    elif question_type == 'food_do_disease':\n",
    "        desc = [i['m.name'] for i in answers]\n",
    "        subject = answers[0]['n.name']\n",
    "        final_answer = '患有{0}的人建议多试试{1}'.format('；'.join(list(set(desc))[:num_limit]), subject)\n",
    "\n",
    "    elif question_type == 'disease_drug':\n",
    "        desc = [i['n.name'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}通常的使用的药品包括：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'drug_disease':\n",
    "        desc = [i['m.name'] for i in answers]\n",
    "        subject = answers[0]['n.name']\n",
    "        final_answer = '{0}主治的疾病有{1},可以试试'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'disease_check':\n",
    "        desc = [i['n.name'] for i in answers]\n",
    "        subject = answers[0]['m.name']\n",
    "        final_answer = '{0}通常可以通过以下方式检查出来：{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    elif question_type == 'check_disease':\n",
    "        desc = [i['m.name'] for i in answers]\n",
    "        subject = answers[0]['n.name']\n",
    "        final_answer = '通常可以通过{0}检查出来的疾病有{1}'.format(subject, '；'.join(list(set(desc))[:num_limit]))\n",
    "\n",
    "    return final_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "for sql_ in sqls:\n",
    "    question_type = sql_['question_type']\n",
    "    queries = sql_['sql']\n",
    "    answers = []\n",
    "    \n",
    "    \n",
    "    for query in queries: # 运行每一条cypher查询语句\n",
    "        ress = g.run(query).data()\n",
    "        answers += ress\n",
    "    final_answer = answer_prettify(question_type, answers)\n",
    "    if final_answer:\n",
    "        final_answers.append(final_answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'m.name': '肺炎杆菌肺炎', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '大叶性肺炎', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '二硫化碳中毒', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '百日咳', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '放射性肺炎', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '苯中毒', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '大楼病综合征', 'r.name': '诊断检查', 'n.name': '血常规'},\n",
       " {'m.name': '肺炎球菌肺炎', 'r.name': '诊断检查', 'n.name': '血常规'}]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ress"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'通常可以通过血常规检查出来的疾病有二硫化碳中毒；大楼病综合征；肺炎杆菌肺炎；大叶性肺炎；苯中毒；放射性肺炎；肺炎球菌肺炎；百日咳'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
